package com.ttl.web.controller;

import com.ttl.web.utils.AliOSSUtils;
import com.ttl.web.utils.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;

/**
 * 上传文件
 */
@Slf4j
@RestController
public class UploadController {

    @Autowired
    AliOSSUtils aliOSSUtils;

    /**
     * 上传文件OSS接口
     */
    @PostMapping("/api/upload")
    public ResultVo upload(@RequestParam("file") MultipartFile image) throws IOException {
        if (image == null) {
            log.error("上传的文件为空");
            return ResultVo.error("上传失败，文件为空");
        }
        log.info("已上传:{}",image.getName());
        String url = aliOSSUtils.upload(image);
        return ResultVo.success("上传成功",url);
    }

//    //本地储存
//    @PostMapping("/upload")
//    public Result upload(String username,Integer age,@RequestParam("image") MultipartFile image) throws IOException {
//        log.info("已上传:{} {} {}",username,age,image.getName());
//        String fileName = UUID.randomUUID().toString() + Objects.requireNonNull(image.getOriginalFilename()).substring(image.getOriginalFilename().lastIndexOf("."));
//        image.transferTo(new File("c:\\Users\\SkyPower\\Desktop\\code\\java\\Springbootdemo\\src\\main\\resources\\static\\"+image.getName()));
//        return Result.success();
//    }
}
